package com.mybatisplus.mp.aspect;

import com.mybatisplus.mp.myanno.Retry;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

//@Component
@Aspect
/**
 * 乐观锁重试
 */
public class RetryForVersion {

    @Pointcut("@annotation(com.mybatisplus.mp.myanno.Retry)")
    private void retryddd() {
    }

    @Around("retryddd()&&@annotation(retry)")
    public Object retryForVersion(ProceedingJoinPoint joinPoint, Retry retry) throws Throwable {
        int count = 0;
        do {
            count++;
            try {
                joinPoint.proceed(); //执行方法
            } catch (RuntimeException e) {
                System.out.println("乐观锁异常");
                if (count>retry.value()) {
                    throw e;
                }
            }
        } while (true);

    }
}
